package hx.front.manager.editor;

import hx.engine.IHouseEngine;
import hx.engine.ISectorEngine;
import hx.front.manager.ManagerAction;
import hx.model.dto.House;
import hx.model.dto.Sector;

import java.util.ArrayList;
import java.util.Collection;

import org.apache.log4j.Logger;

import com.opensymphony.xwork2.validator.annotations.RequiredStringValidator;

/**
 * Tareas de mantenimiento de un único sector previamente filtrado. Alta,
 * edición y borrado.
 * 
 * @author kineas
 * 
 */
public class SectorEditor extends ManagerAction {

	private static final long serialVersionUID = 1L;

	private Logger log = Logger.getLogger(SectorEditor.class);
	
	private ISectorEngine sectorEngine;
	private IHouseEngine houseEngine;

	private String id;
	private String name;
	private Integer coordX;
	private Integer coordY;
	private Integer depth;
	private Integer production;
	private Integer defenseBonus;
	private Integer happiness;

	private String houseId;
	private String loyaltyId;

	private Collection<House> houseList = new ArrayList<House>();
	
	@Override
	public String execute() throws Exception {
		log.info("Entrando en SectorEditor");
		loadCollections();
		
		if(getId()!=null)
			fillForm(getId());
		
		return BEFORE_EDIT;
	}
	
	@Override
	protected void fillForm(String id) {
		Sector sector = sectorEngine.getById(id);
		setName(sector.getName());
		setCoordX(sector.getCoordX());
		setCoordY(sector.getCoordY());
		setDepth(sector.getDepth());
		setDefenseBonus(sector.getDefenseBonus());
		setHappiness(sector.getHappiness());
		setHouseId(sector.getHouse().getId());
		setLoyaltyId(sector.getLoyalty().getId());
		setProduction(sector.getProduction());
	}

	@Override
	protected void loadCollections() throws Exception {
		houseList = houseEngine.findAll();
	}

	@Override
	public String updateCreate() throws Exception {
		loadCollections();
		Sector sector = fillSector();
		sectorEngine.saveOrUpdate(sector);
		return SUCCESS;
	}

	/**
	 * Rellena con los datos del formulario un objeto {@link Sector} y lo
	 * devuelve.
	 * 
	 * @return El sector a persistir.
	 */
	private Sector fillSector() {
		Sector sector = new Sector();
		if(getId()==null)
			sector.setId(getCoordX() + "," + getCoordY());
		else sector.setId(getId());
		sector.setName(getName());
		sector.setCoordX(getCoordX());
		sector.setCoordY(getCoordY());
		sector.setDepth(getDepth());
		sector.setDefenseBonus(getDefenseBonus());
		sector.setDisputed(false);
		sector.setHappiness(getHappiness());
		sector.setHouse(new House(getHouseId()));
		sector.setProduction(getProduction());
		sector.setLoyalty(new House(getLoyaltyId()));
		return sector;
	}


	@Override
	public void validate() {
		try {
			loadCollections();
			if(getId()!=null)
				fillForm(getId());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		super.validate();
	}
	
	// GETTERS AND SETTERS
	public ISectorEngine getSectorEngine() {
		return sectorEngine;
	}

	public void setSectorEngine(ISectorEngine sectorEngine) {
		this.sectorEngine = sectorEngine;
	}

	@RequiredStringValidator(message="Nombre es obligatorio")
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Integer getCoordX() {
		return coordX;
	}

	public void setCoordX(Integer coordX) {
		this.coordX = coordX;
	}

	public Integer getCoordY() {
		return coordY;
	}

	public void setCoordY(Integer coordY) {
		this.coordY = coordY;
	}

	public Integer getDepth() {
		return depth;
	}

	public void setDepth(Integer depth) {
		this.depth = depth;
	}

	public Integer getProduction() {
		return production;
	}

	public void setProduction(Integer production) {
		this.production = production;
	}

	public Integer getDefenseBonus() {
		return defenseBonus;
	}

	public void setDefenseBonus(Integer defenseBonus) {
		this.defenseBonus = defenseBonus;
	}

	public Collection<House> getHouseList() {
		return houseList;
	}

	public void setHouseList(Collection<House> houseList) {
		this.houseList = houseList;
	}

	public IHouseEngine getHouseEngine() {
		return houseEngine;
	}

	public void setHouseEngine(IHouseEngine houseEngine) {
		this.houseEngine = houseEngine;
	}

	public Integer getHappiness() {
		return happiness;
	}

	public void setHappiness(Integer happiness) {
		this.happiness = happiness;
	}

	public String getHouseId() {
		return houseId;
	}

	public void setHouseId(String houseId) {
		this.houseId = houseId;
	}

	public String getLoyaltyId() {
		return loyaltyId;
	}

	public void setLoyaltyId(String loyaltyId) {
		this.loyaltyId = loyaltyId;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

}
